Skip to main content

This section is under development

iRidi API usage examples

Examples of using the iRidi JavaScript API, with descriptions.

Updated: 26.7.2023

Tip

Parent language: JavaScript

Language version: 1.5

Specification: ECMAScript Edition 3

Yes, our version of JS is far from new and does not support newer constructs, but this functionality is sufficient for a Panel Project in 99% of cases. For the remaining percentage, there are converters such as https://babeljs.io} and, of course, the developer's experience and skills

Report an error

Drivers (DDK, Drivers API)​

Examples of creating drivers and changing their settings using the iRidi API

Custom TCP server in an iRidi client​

A Custom Server based on the AV & Custom Systems driver can accept connections from clients based on AV & Custom Systems and third-party applications, process data sent by a client, and send commands to it.

Remember that a server running in the application works only while the application is running. It cannot store a database, unlike the dedicated iRidi Pro solution.

A "server in a client" has very limited uses and can only be used to synchronize certain data between control panels when installing iRidi Pro is not possible for some reason.


// TCP server on port 5678
function start_tcp_server(server_name)
{
// name, port, max connections
var name = server_name;
var port = 5678;
var max_clients = 10;
var tcp_device = IR.CreateDevice(IR.DEVICE_CUSTOM_SERVER_TCP, name, "", port, max_clients);

// start server
tcp_device.Connect();

// inform when the server online
IR.AddListener(IR.EVENT_ONLINE, tcp_device, function(text)
{
IR.Log("Server is ONLINE");
});

// register connected client
IR.AddListener(IR.EVENT_ACCEPT, tcp_device, function(id)
{
IR.Log(tcp_device + " ACCEPT CLIENT "+id);
});

// process received data
IR.AddListener(IR.EVENT_RECEIVE_TEXT, tcp_device, function(text)
{
//IR.Log("INPUT DATA: "+text);
var strings = text.split('\r\n');
var string;
while(string = strings.shift())
{
var parts = string.split(":"); // parse the string with ":" devider
switch(parts[0])
{
case "LOG": // if the first substring of received data = "LOG"
IR.GetItem("Page 1").GetItem("Item 1").GetState(0).Text = IR.GetVariable("System.Time.24")+" "+parts[1];
IR.Log(IR.GetVariable("System.Time.24")+" "+parts[1]);
break;
case "ERROR": // if the first substring = "ERROR"
IR.GetItem("Page 1").GetItem("Item 2").GetState(0).Text = IR.GetVariable("System.Time.24")+" "+parts[1];
IR.Log(IR.GetVariable("System.Time.24")+" "+parts[1]);
break;
case "DATABASE": // if the first substring = "DATABASE"
IR.Log(parts[1]);
break;
}
}
});
}

// start the server when project starts
start_tcp_server("Custom Server TCP");

Interface (GUI API)​

Examples of controlling page, popup, and graphical item properties using the iRidi API

Animating items using IR.Tween​

A tweener determines the value of a variable over a specified interval using a formula. It allows you to animate an object moving over a specified interval along a specified curve.

We will use a tweener to make a popup displayed on a page slide smoothly in from beyond the screen edge. We will use a linear tweener so that the popup moves in a straight line.


function ShowTop()
{
TopTween("Popup 1", 820, 34, 250); // popup, X, Y, moving range from Top
}

////////////////////////////////////////////////////////////////////////////////
// Creating Effects
////////////////////////////////////////////////////////////////////////////////
var timer = 0;
var speed = 20; // showing speed

function TopTween(popup, X, Y, range)
{
IR.ShowPopup(popup);
IR.GetItem(popup).X = X;
var Tm = IR.SetInterval(1, function()
{
timer++;
var Tween = IR.Tween("TWEEN_LINEAR", timer, 0, range, speed);
IR.GetItem(popup).Y = Y+Tween;
if (timer > speed) IR.ClearInterval(Tm); timer = 0;
});
}

Font control​

You can assign a font to an item or change the font size only through the state property .FontId.

FontId is a font identifier that stores a pointer to the font and its size. You cannot change the font size separately.

We will create a new font and associate it with an item state through FontId. It is important to use identifiers that do not already exist in the project when creating new fonts (the editor also creates its own system fonts using identifiers starting at 1). To obtain an unused identifier, use the method

IR.FreeFontID;

Specify which font file to use as the base and which size to set. Assign the new font an identifier that has not previously been used in the project:


var Font=IR.FreeFontID; //Get free font ID
IR.CreateFont(Font, "tahoma.ttf", IR.GetItem("Страница 1").GetState(0).FontSize+10);//Create new font with new size and id
IR.GetItem("Страница 1").GetState(0).FontId=Font;//Set new font to the state

Notifications (Push API)​

Sending a push notification to a control panel. The notification is sent by the server (there are no other options in v1.0.10).

  1. Create control panel groups

By default, no control panel receives push notifications until you place it in a group of panels that can receive push notifications.

Groups are configured on the website; see the instructions.

Each group has an ID and a name. You will use the group ID to specify which panels should receive a notification.

  1. Configure a server script to send notifications

In a server project, create a new script file. Add a condition for sending the notification and the push notification command. A push notification can be triggered by a change to any server variable, whether associated with a driver or virtual.

Example: We will send a notification when the "Address 1" channel of the "KNX" driver receives the value 1. The notification will be received by the panel group with ID = 1.


function sendPush (text, group)
{
IR.SendPush(text, "", group);
// text - message that will be displayed in notification
// group - group of Panels seted up in Cloud Object properties on iRidi Mobile web site
}

IR.AddListener(IR.EVENT_TAG_CHANGE, IR.GetDevice("KNX"), function(name, value)
{
if (name == "Address 1" && value == 1)
{
sendPush("Leakage in the kitchen! The water supply is turned off", 1);
}
});

Note

No additional settings or scripts are required on the control panel; it will receive the notification if it belongs to the specified group.